\documentclass[11pt]{article}
%% \topmargin -1.5cm        % read Lamport p.163
%% \oddsidemargin -0.04cm   % read Lamport p.163
%% \evensidemargin -0.04cm  % same as oddsidemargin but for left-hand pages
%% \textwidth 16.59cm
%% \textheight 21.94cm 
%$ %\pagestyle{empty}       % Uncomment if don't want page numbers
\parskip 7.2pt           % sets spacing between paragraphs
%% %\renewcommand{\baselinestretch}{1.5} % Uncomment for 1.5 spacing between lines
\parindent 0pt		 % sets leading space for paragraphs

\usepackage{fullpage}
\usepackage{listings} % For source code
\usepackage[usenames,dvipsnames]{color} % For colors and names
\usepackage[pdftex]{graphicx}
\usepackage{subfigure}

\usepackage{fixltx2e}

\definecolor{mygrey}{gray}{.96} % Light Grey
\lstset{ 
        language=[ISO]C++,              
        tabsize=3,                                                     
        basicstyle=\tiny,               
        numbers=left,                   
        numberstyle=\tiny,              
        stepnumber=2,                   
        numbersep=5pt,                  
        backgroundcolor=\color{white}, 
        %showspaces=false,              
        %showstringspaces=false,        
        %showtabs=false,                
        frame=single,                    
        tabsize=3,                          
        captionpos=b,                   
        breaklines=true,                
        breakatwhitespace=false,        
        %escapeinside={\%*}{*)},        
        commentstyle=\color{BrickRed}   
}

\title{Northeastern University \\
  Department of Electrical and Computer Engineering \\
  - \\
  ECE5667 \\
  Lab 4 - FIR and IIR Filter Design and Analysis}
\date{\today}
\author{
  Instructor: Deniz Erdogmus \\
  TA: Hooman Nezamfar \\
  Lab Partner: Andrew Lai \\
  - \\
  Author's Name: Paul Ozog
}

\begin{document}

\begin{titlepage}
  \maketitle
  \thispagestyle{empty}
\end{titlepage}

\tableofcontents

\pagebreak

\section{Introduction}
In this lab, we quantitatively and qualitatively analyzed the performance of Finite Impulse Response (FIR) and Infinit Impulse Response (IIR) filters implemented on a ADSP-BF535 EZ-Kit Lite.  We used the \texttt{Talkthough} program included in VisualDSP++ to implement the filters using small software circular buffers.

\section{Results and Analysis}

\subsection{Part I}
\subsubsection{Prelab Programming}
The Part I FIR is given by the following time-domain difference equation of a {6}\textsuperscript{th} order filter:

\begin{equation}
  y[n] = 0.5x[n] + 0.25x[n-1] + 0.125x[n-2] + 0.0625x[n-3] + 0.03125x[n-4]  
\end{equation}

Because the above equation is very similar to Lab 3's reverb FIR, the audio processing, \texttt{Process\_Audio\_Data()}, did not have to be modified for this section of the lab.  Only the constant N (the distance in samples between two adjacent taps) had to be changed along with the filter equation coefficients:
\begin{verbatim}
#define N 1
float a=0.5, b=0.25, c=0.125, d=0.0625, e = .03125;  
\end{verbatim}

Everything else stayed the same for Part I. 

\subsubsection{Exercise 1}

We used matlab to simulate the response of the filter.  It's clear that FIR's time-domain difference equation may be expressed in the Z-domain as a polynomial:

\begin{equation}
  H(z) = \frac{Y(z)}{X(z)} = \sum_{k=0}^{N-1} {\bf B}_kz^{-k}
\end{equation}

Where {\bf B} is the coefficient vector in the time domain difference equation above \texttt{[.5 .25 .125 ...]}


Therefore, we may use the \texttt{freqz} with {\bf B} as shown above.  This gives the magnitude and frequency response shown in Figure \ref{fig:fir}.  This behavior is that of a low-pass filter.

To experimentally verify this response, we meausred the actual attenuation and phase shift of the output for various input sinusoids.  The scope's displays for different frequencies are shown in Figure \ref{fig:resp1}.  One should note that the phase shift for the last entry was immeasurable because the output amplitude was so heavily attenuated.  

\begin{table}[h]
  \caption{Exercise 1 Results}
  \centering
  \begin{tabular} {c c c}
    \hline\hline
    Input Frequency (Hz) & Gain (dB) & Phase Shift (rad)\\ [0.5ex]
    \hline
    353.4 & -0.088 & .72 \\
    1201.0 & -6.8  & 2.94 \\
    2427.0  & -18.35 & xx  \\ [1ex]

    \hline
  \end{tabular}
  \label{table:ex1}
\end{table}

\begin{figure}[hp]
  \begin{center}
    \subfigure[Part I FIR filter response]{
      \includegraphics[scale=0.6]{part1.pdf}
      \label{fig:fir}}
    \subfigure[Input Freq. = 353 Hz]{
      \includegraphics[scale=0.3]{PRINT_09.PNG}
      \label{fig:resp1}}
    \subfigure[Input Freq. = 12.01kHz]{
      \includegraphics[scale=0.3]{PRINT_10.PNG}
      \label{fig:resp2}}
    \subfigure[Input Freq. = 23.98kHz]{
      \includegraphics[scale=0.3]{PRINT_11.PNG}
      \label{fig:resp3}}
    \caption{Results for Part 1 - Exercise 1}
  \end{center}
\end{figure}

To measure the noise level, we increased the function generator output frequency to be much higher the Nyquist rate of 24 kHz.  This resulted in a flat + noise wave with peak-to-peak amplitude of 120 mV.

As one can see from these datapoints, this filter is low-pass.  For the most part, there is strong correlation between the Matlab plot of Figure \ref{fig:fir} and actual results.  However, the phase is consistently off by about .7 radians, which is probably due to overall lag in the DSP board.

\subsubsection{Exercise 2}
To experiment with the low-pass, we connected a mic to the input of the ADSP-BF535 board and wrote the input-selector CSR so that we may use a microphone.  After experimenting with voice signals, the effect of the FIR was hard to notice.  We think this is due to the fact that humans notice low-end frequencies much more than high-end ones.  Since the FIR was low-pass, the ``important'' frequencies were preserved, whereas the subtle high-end frequencies were cut.

\subsubsection{Exercise 3}
To implement the filter for a stereo input, we simply had to first change the input selector register to Line-In.  Then, we modified the \texttt{Process\_Audio\_Data()} function to have a buffer for both channels:
\begin{verbatim}
...
//Delay the right channel, pass the left channel
sLeft_Channel_Out  = CircBuffLeft[(ind+1) % ARRAY_SIZE];
sRight_Channel_Out = CircBuffRight[(ind+1) % ARRAY_SIZE];
...
\end{verbatim}

Using this setup, the sound quality from an MP3 player sounded especially warm.  Instruments like cymbals were hard to hear.  This makes sense because the high-end frequencies were attenuated in our low-pass filter.  

\subsubsection{Exercise 4}
We chose to implement a filter for Exercise 4 with the following specifications:
\begin{verbatim}
Cutoff Freq = 14.4kHz
Order       = 8
Type        = high pass  
\end{verbatim}

The response of the filter is shown in Figure \ref{fig:ex4}.  The coefficients were determined using the ``Realize Model'' tool in Matlab's \texttt{fdatool} program.  This resulted in a filter with these coefficients:

\begin{verbatim}
  [0.108 0.124 -0.35 0.475 -0.35 0.124 0.108]
\end{verbatim}

Also, the modifications to \texttt{Process\_Audio\_Data()} only involved adding 2 taps to the filter in the previous exercises.

\begin{figure}[h]
  \begin{center}
    \includegraphics[scale=0.6]{part1-ex4.pdf}
    \caption{Exercise 4 FIR filter response}
    \label{fig:ex4}
  \end{center}
\end{figure}

When running this high-pass filter on audio from an MP3 player, the sound quality dropped dramatically.  This is because most of the information discernible to the human ear is in frequencies at the lower end of the audio spectrum.  Therefore, since our filter removed those essential frequencies, the audio output showed little resemblance to an unfiltered audio signal.  
\subsection{Part II}

\subsubsection{Prelab Programming}
The general IIR filter is given in this form in the time domain: 
\begin{equation}
  y[n] = b_0 x[n] + \sum_{k=1}^{N}a_ky[n-k]
\end{equation}

Therefore, to implement this on the ADSP-BF535 board, we had to buffer the {\it output} instead of the input.  
We therefore modified \texttt{Process\_Audio\_Data()} as follows:

\begin{verbatim}
...
CircBuffLeft = b*sLeft_Channel_In  + a1*LCH_Out[i1] + a2*LCH_Out[i2];
...  
\end{verbatim}

\subsubsection{Exercise 1}
One should note that actual filter to implement was given as a rational function of {z}\textsuperscript{-1} in the Z-domain:
\begin{equation}
  H(z) = \frac{Y(z)}{X(z)} = \frac{K}{1+0.9z^{-1}+0.81z^{-2}}
\end{equation}

The coefficients {a}\textsubscript{k} and {b}\textsubscript{0} can be found by taking the inverse Z-transform of the above equation.
This results in the following difference equation:
\begin{equation}
  y[n] = K x[n] - .9 y[n] - .81 y[n-2]
\end{equation}

To find the value of {\it K} such that the max of 
\begin{math}
  |H(z)|_{dB}
\end{math}
is zero, we simply took the inverse of the maximum of H when the numerator is 1:

\begin{verbatim}
  [H, w] = freqz(1, [1 0.9 0.81]);
  K = max(abs(H))^-1; % 0.1646
\end{verbatim}

Using K = 0.1646, we obtain a magnitude and phase response of the IIR filter, which is shown in Figure \ref{fig:iir}.
The corresponding pole-zero plot is shown in Figure \ref{fig:polezero}.
Note that the magnitude response peaks at 0 dB attenuation.

\begin{figure}[h]
  \begin{center}
    \subfigure[IIR filter from Part II]{
      \includegraphics[scale=0.5]{part2-ex1-mag-freq-resp.pdf}
      \label{fig:iir}}
    \subfigure[Corresponding pole-zero plot]{
      \includegraphics[scale=0.5]{part2-ex1.pdf}
      \label{fig:polezero}}
  \end{center}
\end{figure}

\subsubsection{Exercise 2}
We decided to test the filter with a sinusoid at the peak frequency of the IIR filter, which is about 16.0 kHz.  
Then, we deviated from the center frequency and made sure there was an appropriate phase shift and attenuation.
The results are shown in Figure \ref{fig:part2}.

\begin{figure}[h]
  \begin{center}
    \subfigure[Input Freq. = 15.97 kHz]{
      \includegraphics[scale=0.4]{PRINT_00.PNG}
      \label{fig:resp4}}
    \subfigure[Input Freq. = 14.95kHz]{
      \includegraphics[scale=0.4]{PRINT_01.PNG}
      \label{fig:resp5}}
    \caption{Results for Part II - Exercise 2}
    \label{fig:part2}}
  \end{center}
\end{figure}

One should note that when {f}\textsubscript{in} is 15.97 kHz, the attenuation is minimal.  
When decreasing the frequency to 14.95 kHz, we get loss of 3 dB, which is supported by Figure \ref{fig:iir}. 
Though not shown in the scope plots, the phase shift of the output increased when the input frequency decreased slightly from the center frequency.  Similarly, the output's phase decreased when the input frequency increased just beyond the center frequency.  However, the pattern reversed when the input frequency decreased past 13.44 kHz and increased past 18.00 kHz.  All of this behavior is once again supported by Figure \ref{fig:iir}.  

\subsubsection{Exercise 3}
Because the voice band primarily ranges from about 300 Hz to 3.4 kHz, the filter resulted in attenuation of the main frequencies of speech.  However, unless the output was compared side-by-side to unfiltered speech, the effect was hard to notice - just as it was for Part I.

\subsubsection{Exercise 4}
To modify the program to use a stereo input, it required a very similar procedure to Part I.  In particular, the input selection register was set to Line-in.  Also, the \texttt{Process\_Audio\_Data()} function was modified to buffer the right channel output:

\begin{verbatim}
...
CircBuffLeft  = b*sLeft_Channel_In   + a1*LCH_Out[i1] + a2*LCH_Out[i2];
CircBuffRight = b*sRight_Channel_In  + a1*RCH_Out[i1] + a2*RCH_Out[i2];
...  
\end{verbatim}

When playing music through the filter, the effect was most profound when keeping the output speaker volume low.  We could more easily hear which frequencies are passed when the rest of the spectrum was too quiet to be heard.  We could hear that high-end instruments like cymbals were most easily heard at the output of this filter.  This makes sense because the center frequency is 16kHz, a relatively high frequency for human hearing. 

\section{Conclusion}
We have successfully implemented IIR and FIR filters on the ADSP-BF535, while quantitatively and qualitatively verifying their performance.  The low-pass FIR given in Part 1 had a cutoff frequency of .24 times the normalized Nyquist frequency, or 5.760 kHz.  This was verified experimentally using a sinewave generator and oscilloscope.  Similarly, we were available to experimentally analyze that the high-pass filter we designed using \texttt{fdatool} worked correctly when applied to stereo audio signals.  Finally, the IIR band-pass filter given in the lab manual performanced as expected.  However, it is worth mentioning that throughout our measurements, there was a near-constant phase difference of approximately .7 radians between what was simulated and Matlab and what was actually measured from the board.  This is probably due to some sort of processing or switching delay in the ADSP-BF535. 

\end{document}
